package org.xtframe.sql.verify;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.xtframe.bean.SystemConfig;
import org.xtframe.sql.SqlDataVerify.VerifyType;
import org.xtframe.sql.PreparedParameter;
import org.xtframe.sql.VerifyBase;
import org.xtframe.util.SystemConfigUtil;

public class CheckVerify implements VerifyBase {
	
	// Sql检查参数（替换、检查）
	public enum CheckParameter {
		CHINESE, TWOBYTE, BLANK, HTML, TRIM, EMAIL, URL, NAME, CHINATEL, QQ, POSTAL, IDENTITY, IP,
		LETTER, BLETTER, SLETTER, NUMLETTER, NUM_LETTER, ZINT, FINT, INT, ZINT0, FINT0, ZDOUBLE,
		FDOUBLE, DOUBLE, ZDOUBLE0, FDOUBLE0
	}

	private String position = "";
	
	private CheckParameter value = null;
	
	public void init(Map<String, Object> data) {
		if(data.containsKey("position")) this.position = data.get("position").toString();
		if(data.containsKey("value")) this.value = CheckParameter.valueOf(data.get("value").toString().toUpperCase());
	}

	public String getPosition() {
		return this.position;
	}

	public VerifyType getXtype() {
		return VerifyType.check;
	}

	public boolean check(Map<String, Object> parameters) {
		return Verify(value, parameters.get(position).toString());
	}

	public String replace(Map<String, Object> parameters, List<PreparedParameter> preparedParameters) {
		return null;
	}
	
	/**
	 * 数据校验
	 * 
	 * @param parameter
	 * @param value
	 * @return
	 */
	public static boolean Verify(CheckParameter parameter, String value) {
		SystemConfig systemConfig = SystemConfigUtil.getSystemConfig();

		if (parameter == CheckParameter.CHINESE) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsChinese());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.TWOBYTE) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIs2Byte());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.BLANK) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsBlank());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.HTML) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsHtml());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.TRIM) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsTrim());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.EMAIL) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsEmail());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.URL) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsUrl());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.NAME) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsName());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.CHINATEL) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsChinaTel());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.QQ) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsQQ());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.POSTAL) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsPostal());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.IDENTITY) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsIdentity());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.IP) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsIp());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.LETTER) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsLetter());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.BLETTER) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsBLetter());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.SLETTER) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsSLetter());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.NUMLETTER) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsNumLetter());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.NUM_LETTER) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsNum_Letter());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.ZINT) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsZInt());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.FINT) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsFInt());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.INT) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsInt());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.ZINT0) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsZInt0());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.FINT0) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsFInt0());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.ZDOUBLE) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsZDouble());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.FDOUBLE) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsFDouble());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.DOUBLE) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsDouble());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.ZDOUBLE0) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsZDouble0());
			return pattern.matcher(value).matches();
		} else if (parameter == CheckParameter.FDOUBLE0) {
			Pattern pattern = Pattern.compile(systemConfig.getVerifyIsFDouble0());
			return pattern.matcher(value).matches();
		} else {
			return true;
		}
	}

}
